import org.newdawn.slick.*;
import org.newdawn.slick.util.*;
import java.util.*;

/**
 * A class that receives user input, parses it to an action
 * and then sends it to the ApplicationStateManager.
 * Currently does NOT support controllers other than keyboard
 *
 * @author dbos
 * @version 2008-04-18
 */
public class InputManager extends InputAdapter
{
    // The ApplicationStateManager to receive any input
    private ApplicationStateManager manager;
    // Maps player keyboard input to in-game commands
    private HashMap<Integer,String> map = new HashMap<Integer,String>();

    /**
     * Constructor for objects of class InputManager
     */
    public InputManager(ApplicationStateManager mgr)
    {
        this.manager = mgr;
        map.put(Input.KEY_W, "up");
        map.put(Input.KEY_A, "left");
        map.put(Input.KEY_D, "right");
        map.put(Input.KEY_S, "ccw");
        map.put(Input.KEY_UP, "up");
        map.put(Input.KEY_DOWN, "down");
        map.put(Input.KEY_TAB, "tab");
        map.put(Input.KEY_LSHIFT, "shift");
        map.put(Input.KEY_RSHIFT, "shift");
        map.put(Input.KEY_LEFT, "left");
        map.put(Input.KEY_RIGHT, "right");
        map.put(Input.KEY_ENTER, "use1");
        map.put(Input.KEY_SPACE, "use2");
        map.put(Input.KEY_ESCAPE, "cancel");
		map.put(Input.KEY_1, "num1");
		map.put(Input.KEY_2, "num2");
		map.put(Input.KEY_3, "num3");
		map.put(Input.KEY_F1, "music");
		map.put(Input.KEY_F2, "sound");
		map.put(Input.KEY_F3, "antilag");
		map.put(Input.KEY_P, "pause");
		map.put(Input.KEY_PAUSE, "pause");
		map.put(Input.KEY_Q, "scoreboard");
    }

    /**
     * overrides method in slick.util.InputAdapter
     * parses pressed key integer to a string representing an initiated
     * action and sends to the app state manager
     *
     * @param  key  the key pressed
     * @param  c  the character of the pressed key
     */
    public void keyPressed(int key, char c) {
		String mapVal = map.get(key);
		if(mapVal != null) {
			String action = "p_"+mapVal;
			manager.forwardInput(action);
		}
    }

    /**
     * overrides method in slick.util.InputAdapter
     * parses released key integer to a string representing a terminiting
     * action and sends to the app state manager
     *
     * @param  key  the key pressed
     * @param  c  the character of the pressed key
     */
    public void keyReleased(int key, char c) {
		String mapVal = map.get(key);
		if(mapVal != null) {
			String action = "r_"+mapVal;
			manager.forwardInput(action);
		}
    }

	/**
	* Informs the InputManager that the mouse has been moved.
	* The information is forwarded to the ApplicationStateManager.
	*
	* @param oldx The old x coordinate.
	* @param oldy The old y coordinate.
	* @param newx The new x coordinate.
	* @param newy The new y coordinate.
	*/
	public void mouseMoved(int oldx, int oldy, int newx, int newy) {
		manager.forwardMouseMoved(oldx, oldy, newx, newy);
	}

	/**
	* Informs the InputManager that a mouse button has been pressed.
	* The information is forwarded to the ApplicationStateManager.
	*
	* @param button The old mouse button pressed.
	* @param y The y coordinate.
	* @param x The x oordinate.
	*/
	public void mousePressed(int button, int x, int y) {
		manager.forwardMousePressed(button, x, y);
	}

}